package b_state.a_origin;

/**
 * Created by wug on 2016/6/13 0013.
 */

/**
 * 农夫山泉自动售水机
 */
public class WaterMachine {
    /**
     * 没钱
     */
    final static int NO_MONEY = 0;
    /**
     * 有钱
     */
    final static int HAVE_MONEY = 1;
    /**
     * 已出货
     */
    final static int SOLD = 2;
    /**
     * 已售完
     */
    final static int SOLD_OUT = 3;

    int state = SOLD_OUT;
    /**
     * 机器中水的数量
     */
    int num = 0;
    /**
     * 机器中钱的数量
     */
    int money = 0;


    /**
     * 工人放货
     */
    public void workerPut() {
        num = 10;
    }

    /**
     * 投钱
     * @param num
     */
    public void insertMoney(int num) {
        if (state == NO_MONEY) {
            state = HAVE_MONEY;
            money = num;
        } else if (state == HAVE_MONEY){
            money += num;
        } else if (state == SOLD) {
            state = HAVE_MONEY;
            money += num;
        } else if (state == SOLD_OUT) {
            state = HAVE_MONEY;
            money = num;
        }
        System.out.println("你投入" + num + "元钱,现在机器里有" + money + "元钱");
    }

    /**
     * 按下出货按钮
     */
    public void pressSellBtn() {
        if (state == NO_MONEY) {
            System.out.println("没钱买啥？");
        } else if (state == HAVE_MONEY){
            if (money>=5) {
                if (num >0) {
                    money-=5;
                    num-=1;
                    state = SOLD;
                    System.out.println("已出货，还剩下" + num + "瓶水!");
                } else {
                    state = SOLD_OUT;
                    System.out.println("sorry!卖光啦!!");
                }
            } else {
                System.out.println("钱不够！");
            }
        } else if (state == SOLD) {
            if (money >= 5) {
                if (num >0) {
                    money-=5;
                    num-=1;
                    System.out.println("已出货，还剩下" + num + "瓶水!");
                } else {
                    state = SOLD_OUT;
                    System.out.println("sorry!卖光啦!!");
                }
            } else {
                System.out.println("钱不够！");
            }
        } else if (state == SOLD_OUT) {
            System.out.println("sorry!卖光啦!!");
        }
    }

    /**
     * 摇动退款摇杆
     */
    public void refundRocker() {
        if (state == NO_MONEY) {
            System.out.println("没有钱怎么退？");
        } else {
            System.out.println("退回" + money + "元钱");
            money = 0;
            state = NO_MONEY;
        }
    }


    /**
     * 为了维护机器的状态我们 在每个用户行为的背后都做了太多的判断
     * 如果这个时候再多一种状态怎么办？比如 想要加个幸运用户 可以免费获得一瓶水
     * 这会需要修改每个用户行为的代码
     * 耦合性实在是太高了
     * 我们需要用设计模式了
     */
}
